package cool.leeson.library.controller.user;


import cool.leeson.library.entity.user.UserCredit;
import cool.leeson.library.service.user.UserCreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("userCredits")
public class UserCreditController {

    @Autowired
    private UserCreditService userCreditService;

    // 查询单个信用记录
    @GetMapping("/{id}")
    public UserCredit queryById(@PathVariable String id) {
        return userCreditService.queryById(id);
    }

    // 查询单个用户的信用记录
    @GetMapping("/userId/{userId}")
    public UserCredit queryByUserId(@PathVariable String userId) {
        return userCreditService.queryByUserId(userId);
    }

    // 查询分页数据
    @GetMapping("/page")
    public List<UserCredit> queryAllByLimit(@RequestParam int offset, @RequestParam int pageSize) {
        Pageable pageable = PageRequest.of(offset, pageSize);
        UserCredit userCredit = new UserCredit(); // 创建一个空的查询条件对象
        return userCreditService.queryAllByLimit(userCredit, pageable);
    }

    @GetMapping("/getAll")
    public List<UserCredit> getAll() {
        return userCreditService.getAll();
    }

    // 统计总行数
    @GetMapping("/count")
    public long count() {
        UserCredit userCredit = new UserCredit(); // 创建一个空的查询条件对象
        return userCreditService.count(userCredit);
    }

    // 新增信用记录
    @PostMapping
    public int insert(@RequestBody UserCredit userCredit) {
        return userCreditService.insert(userCredit);
    }

    // 批量新增信用记录
    @PostMapping("/batch")
    public int insertBatch(@RequestBody List<UserCredit> userCredits) {
        return userCreditService.insertBatch(userCredits);
    }

    // 批量新增或更新信用记录
    @PostMapping("/batch-or-update")
    public int insertOrUpdateBatch(@RequestBody List<UserCredit> userCredits) {
        return userCreditService.insertOrUpdateBatch(userCredits);
    }

    // 更新信用记录
    @PutMapping
    public int update(@RequestBody UserCredit userCredit) {
        return userCreditService.update(userCredit);
    }

    // 删除信用记录
    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable String id) {
        return userCreditService.deleteById(id);
    }
}
